
namespace SharpTalk.Core.Base
{
    using System;
    using System.Runtime.Serialization;
    using Interfaces;

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T">The type of the content of the message</typeparam>
    [Serializable]
    [DataContract]
    public abstract class MessageBase<T> 
        : IMessage<T>, IEquatable<MessageBase<T>>
    {
        private Guid _owner;
        private DateTime _dateTime;
        private T _content;
        private Guid _guid;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        protected internal MessageBase(Guid guid)
        {
            _guid = guid;
            _dateTime = DateTime.Now;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        protected internal MessageBase()
            :this(Guid.NewGuid())
        {
        }

        /// <summary>
        /// Obtains or define the owner
        /// </summary>
        [DataMember]
        public Guid Owner
        {
            get { return _owner; }
            set { _owner = value; }
        }

        /// <summary>
        /// Obtains or defines the datetime of the message
        /// </summary>
        [DataMember]
        public virtual DateTime DateTime
        {
            get { return _dateTime; }
            set { _dateTime = value; }
        }
        /// <summary>
        /// Obtain or defines the content of the message
        /// </summary>
        [DataMember]
        public virtual T Content
        {
            get { return _content; }
            set { _content = value; }
        }
        /// <summary>
        /// Obtain the Guid
        /// </summary>
        [DataMember]
        public Guid Guid
        {
            get { return _guid; }
            set { _guid = value; }
        }
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(MessageBase<T> other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other._guid.Equals(_guid);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (MessageBase<T>)) return false;
            return Equals((MessageBase<T>) obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return _guid.GetHashCode();
        }

        public static bool operator ==(MessageBase<T> left, MessageBase<T> right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(MessageBase<T> left, MessageBase<T> right)
        {
            return !Equals(left, right);
        }
    }
}